<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="16-animate.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        body {
            background-color: black;
        }

        .box {
            border-radius: 50px;
            width: 500px;
            height: 50px;
            background-color: #fff;

        }

        ul {
            position: relative;
        }

        li {
            list-style: none;
            float: left;
            padding: 20px;
            cursor: pointer;
        }

        ul li:nth-child(1) {
            color: red;
        }


        img {
            position: absolute;
            top: 2px;
            left: 2px;
            width: 40px;
            height: 40px;
        }
    </style>
</head>

<body>
    <div class="box">
        <img src="../01-Dom-标准接口/4-操作元素/image/gr.gif" alt="">
        <ul>

            <li>你好</li>
            <li>再见</li>
            <li>哈喽</li>
            <li>还好</li>
            <li>希望</li>
            <li>明天</li>
        </ul>
    </div>
    <script>
        var ul = document.querySelector('ul')
        var lis = ul.querySelectorAll('li')
        var img = document.querySelector('img') 
        var num = 0
        for (var i = 0; i < lis.length; i++) {
            // 鼠标经过把当前li的位置作为目标值
            lis[i].addEventListener('mouseenter', function () {
                animate(img, this.offsetLeft)
            })
            // 鼠标离开就回到起始位置
            lis[i].addEventListener('mouseleave', function () {
                animate(img, num)//只要不点击 鼠标离开后起始位置就还是0 num = 0 互不影响
            })
            // 鼠标一点击就把当前点击li位置保存起来 重新当做动画函数的起始位置
            lis[i].addEventListener('click', function () {
                num = this.offsetLeft
                animate(img, num)

            })
        }

    </script>
</body>

</html>